//
//  ForgotPasswordViewController.m
//  OrderSeat
//
//  Created by haiming on 13-10-8.
//  Copyright (c) 2013年 wangwei. All rights reserved.
//

#import "ForgotPasswordViewController.h"
#import "EnterNewPWViewController.h"
#import "LoginViewController.h"

#define FORGOT_TAG  1001
#define NEXT_TAG    1002

@implementation ForgotPasswordViewController

- (id)initWithPassType:(PassType)passType
{
    self = [super initWithNibClass:[ForgotPasswordViewController class]];
    if (self) {
        _passType = passType;
        _dataSource = [[ForgotPasswordDataSource alloc] initWithVC:self];
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self setupUI];
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [[AppDelegate sharedInstance] hideTabBar:self.tabBarController];
}

-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [[AppDelegate sharedInstance] showTabBar:self.tabBarController];
}

- (void)setupUI
{
    [self createLeftButton];
    
    NSString *title = _passType == Forgot ? @"找回密码" : @"修改密码";
    [self createNavigation:title];
    
    if (_passType == Forgot)
        [self createRightButton:@"登录"];
    
    if (_passType == Update)
        _titleLabel.text = @"修改验证手机";
    
    [self.view addTarget:self action:@selector(viewTapped:)];
    [_contentView drawBorder];
}

- (void)rightButtonClick:(id)sender
{
    [self pushOrPopViewController:[LoginViewController class]];
}

-(void)viewTapped:(UITapGestureRecognizer*)tapGr
{
    [self.phoneTF resignFirstResponder];
    [self.verifyTF resignFirstResponder];
}

- (IBAction)buttonAction:(id)sender
{
    UIButton* btn = (UIButton*)sender;
    _phoneNumber = _phoneTF.text;
    _verifyNumber = _verifyTF.text;
    
    if (btn.tag == FORGOT_TAG) {
        [_dataSource validateTel];
    }
    
    if (btn.tag == NEXT_TAG) {
        if(![self checkFrom]) return;
        [_dataSource validateCode];
    }
}

- (BOOL)checkFrom
{
    if (_phoneTF.text.length <= 0) {
        [AppDelegate showMessage:@"手机号码不能为空！"];
        return NO;
    }
    
    if (_verifyTF.text.length <= 0) {
        [AppDelegate showMessage:@"验证码不能为空!"];
        return NO;
    }
    
    return YES;
}

@end

//==========================================================================================
#pragma ===================数据源类实现=======================================================
//==========================================================================================

#define SEND_SMS_TAG 1001
#define VALIDATE_TEL_TAG 1002
#define VALIDATE_CODE_TAG 1003

@implementation ForgotPasswordDataSource

- (id)initWithVC:(id)Controller
{
    self = [super init];
    if(self) {
        self.Controller = Controller;
    }
    
    return self;
}

- (void)sendSMS
{
    NSString *url = [NSString stringWithFormat:API_MESSAGE,_Controller.phoneNumber];
    [self get:url tag:SEND_SMS_TAG];
}

- (void)validateTel
{
    [SVProgressHUD show];
    NSString *url = [NSString stringWithFormat:API_TEL_ISREGISTER,_Controller.phoneNumber];
    [self get:url tag:VALIDATE_TEL_TAG];
}

- (void)validateCode
{
    [SVProgressHUD show];
    NSString *url = [NSString stringWithFormat:API_VALIDATE_CODE, _Controller.verifyNumber, _Controller.phoneNumber];
    [self get:url tag:VALIDATE_CODE_TAG];
}

- (void)requestFinished:(ASIHTTPRequest *)request
{
    CLog(@"%@", request.responseString);
    
    NSDictionary *json = [request.responseData JSONValue];
    if ([[json objectForKey:@"error_code"] intValue] != 0) {
        return;
    }
    
    NSDictionary *data = [json objectForKey:@"data"];
    [self processRequest:data withTag:request.tag];
}

- (void)processRequest:(NSDictionary *)data withTag:(int)tag
{
    int state = [[data objectForKey:@"state"] intValue];
    
    if (tag == VALIDATE_TEL_TAG) {
        if (state == 1) {
            [self sendSMS];
        }
        if (state == 2) {
            [SVProgressHUD dismissWithError:@"您填写的手机号码未注册，请前去注册"];
        }
    }
    
    if (tag == SEND_SMS_TAG) {
        if (state == 1) {
            [SVProgressHUD dismissWithSuccess:@"验证码已发送，请查看手机短信"];
        }
    }
    
    if (tag == VALIDATE_CODE_TAG) {
        if (state == 1) {
            [SVProgressHUD dismiss];
            
            [AppSetting setTel:_Controller.phoneNumber];
            EnterNewPWViewController *newVC = [[EnterNewPWViewController alloc] initWithPassType:_Controller.passType];
            [_Controller pushViewController:newVC];
        }
        if (state == 2) {
            [SVProgressHUD dismissWithError:@"验证码验证失败"];
        }
    }
}

@end
